<?php
/*
 *	  osmetadata.php
 *	  
 *	  Copyright 2010 Vitaliy Rakitin <virak@ukr.net>
 *	  
 *	  This program is free software; you can redistribute it and/or modify
 *	  it under the terms of the GNU General Public License as published by
 *	  the Free Software Foundation; either version 2 of the License, or
 *	  (at your option) any later version.
 *	  
 *	  This program is distributed in the hope that it will be useful,
 *	  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	  GNU General Public License for more details.
 *	  
 *	  You should have received a copy of the GNU General Public License
 *	  along with this program; if not, write to the Free Software
 *	  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *	  MA 02110-1301, USA.
 */
 
/*
 Метаданные НЕ ДОЛЖНЫ содержать никакой информации о данных, которые будут 
 вводиться, храниться, использоваться с помощью последних!
*/
 
//require_once($_SERVER['DOCUMENT_ROOT']."/classes/System/osobjects.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/metadata/osmetadictionaries.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/DB/osmysqldb.php");

class osmetadata {
	
	private $dictionaries;
	//private $objects;
	public $database;
	private $databasetype = "mysql";
	
	function __construct() {
		$this->dictionaries = new osmetadictionaries();
		//$this->objects = new osobjects();
	}
	
	private function __clone(){}
	
	function open($str) {
		$_SESSION["metadata"] = unserialize($str);
	}
	
	function openfromfile($filename) {
		$handle = fopen($filename, "rb");
		$this->open(fread($handle, filesize($filename)));
		fclose($handle);
	}
	
	function connectDB() {
		switch ($this->databasetype) {
			case "mysql": {$this->database = new osmysqldb();break;}
		}
	}
	
	function createDB() {
		$this->updateDB();
	}
	
	function clearDB() {
		$this->database->dropAllTables();
	}
	
	function updateDB() {
		foreach ($this->dictionaries->getall() as $dictionary) {
			unset($fieldsList);
			$fieldsList["id"] = "varchar(36) PRIMARY KEY";
			foreach ($dictionary->fields->getall() as $field) {
				$str = $field->type."(".$field->length.")";
				$fieldsList[$field->uuid] = $str;
			}
			$this->database->createTable($dictionary->uuid, $fieldsList);
			
			foreach ($dictionary->tabulars->getall() as $tabular) {
				unset($fieldsList);
				$fieldsList["id"] = "varchar(36) PRIMARY KEY";
				$fieldsList["owner"] = "varchar(36)";
				foreach ($tabular->fields->getall() as $field) {
					$str = $field->type."(".$field->length.")";
					$fieldsList[$field->uuid] = $str;
				}
				$this->database->createTable($tabular->uuid, $fieldsList);
			}
		}
	}
	
	function clean() {
		$_SESSION["metadata"] = new osmetadata();
	}
	
	function save() {
		return serialize($this);
	}
	
	function savetofile($filename) {
		$content = $this->save();
		if (!$handle = fopen($filename, "wb")) {
			 echo "Не могу открыть файл ($filename)";
			 exit;
		}
		if (fwrite($handle, $content) === FALSE) {
			echo "Не могу произвести запись в файл ($filename)";
			exit;
		}
		//echo "Ура! Записали ($content) в файл ($filename)<br>";
		echo "Ура! Записали в файл ($filename)<br>";
		fclose($handle);
	}
	
	function __get($name) {
		switch ($name) {
			//case "objects": { return new osobjects(); break;}
			//case "dictionaries": { if (!$this->dictionaries)return new osdictionaries(); break;}
			case "dictionaries": { return $this->dictionaries; break;}
			//case "objects": { return $this->objects; break;}
		}
	}
	
}

session_start();

if (!array_key_exists("metadata", $_SESSION)) {
	$_SESSION["metadata"] = new osmetadata();
} else {
	if (!(get_class($_SESSION["metadata"]) == "osmetadata")) {
		$_SESSION["metadata"] = new osmetadata();
	}
}
	$metadata = &$_SESSION["metadata"];
?>
